<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Commons;
use app\admin\model\Qinu;
use think\Db;
use think\Request;

class Link
{
    /**加载编辑友情链接，获取友情链接的id
     * @return string|\think\response\View
     */
    public function edit(Request $request){
        $uid = $request->param('uid',null);
        $pid = $request->param('pid',null);
        $data = Db::view('document',true)
            ->view('model','surface','model.id=document.model','left')
            ->view('link','*','document.id=link.uid','left')
            ->where('document.id',$uid)
            ->find();
        $lanmu = Db::view('document',true)
            ->view('model','surface','model.id=document.model','left')
            ->where('surface','cate')
            ->select();
//        echo '<pre>';
//        print_r($data);exit;
        return view('link_edit',['data'=>$data,'lanmu'=>$lanmu,'pid'=>$pid]);
    }

    /**更新友情链接，获取数据，成功返回1，失败返回原因
     * @return int|string
     * @throws \think\Exception
     */
    public function link_update(Request $request){
        $data = $request->param();
        //验证是否符合
        $num = val($data,[
            'title'=>'require',
            'pid'  =>'require',
            'status'=>'require',
            'sort'  =>'require',
            'uid'   =>'require'
        ]);
        if($num!==1){
            return $num;
        }
        //讲document表数据单独取出
        $document['title'] = $data['title'];
        $document['pid'] = $data['pid'];
        $document['status'] = $data['status'];
        $document['dir'] = $data['dir'];
        $document['sort'] = $data['sort'];
        //删除$data的多余数据
        unset($data['title']);
        unset($data['pid']);
        unset($data['status']);
        unset($data['dir']);
        unset($data['sort']);
        $logo = Qinu::Upfile($_FILES['logo']);
        $link = Db::name('link')->where('uid',$data['uid'])->find();
        if($logo){
            if($link['logo']){
                Qinu::DeleFile($link['logo']);
            }
            $data['logo'] = $logo;
        }
        $resul=Db::connect();
        if($resul->name('document')->where('id',$data['uid'])->update($document)||$resul->name('link')->where('uid',$data['uid'])->update($data)){
            return 1;
        }else{
            return '更新失败';
        }
    }
    /**加载添加友情链接弹框页面
     * @return \think\response\View
     */
    public function show_add(Request $request){
        $model = $request->param('model');
        $pid = $request->param('pid',null);
        if($pid){
            $lanmus = (new Commons())->getData($pid);
            $lanmu = [];
            $docu   = Db::name('document')->where('id',$pid)->find();
            $lanmus[] = $docu;
            $modeid = (Db::name('model')->where('surface','cate')->find())['id'];
            foreach ($lanmus as $value){
                if($value['model']==$modeid){
                    $lanmu[] = $value;
                }
            }
        }else{
            $lanmu = Db::view('document',true)
                ->view('model','surface','document.model=model.id','left')
                ->where('surface','cate')
                ->select();
        }

        return view('link_add',['model'=>$model,'lanmu'=>$lanmu,'pid'=>$pid]);
    }

    /**添加友情链接接口，获取link表的各项信息，成功返回1，失败返回原因
     * @return int|string
     */
    public function link_add(Request $request){
        $data = $request->param();
        //验证是否符合
        $num = val($data,[
            'title'=>'require',
            'pid'  =>'require',
            'model'=>'require',
            'status'=>'require',
            'sort'  =>'require'
        ]);
        if($num!==1){
            return $num;
        }
        //讲document表数据单独取出
        $document['title'] = $data['title'];
        $document['pid'] = $data['pid'];
        $document['model'] = $data['model'];
        $document['status'] = $data['status'];
        $document['dir'] = $data['dir'];
        $document['sort'] = $data['sort'];
        //删除$data的多余数据
        unset($data['title']);
        unset($data['pid']);
        unset($data['model']);
        unset($data['status']);
        unset($data['dir']);
        unset($data['sort']);
        //获取栏目的uid，文档id
        $uid = Db::name('document')->insertGetId($document);
        $logo = Qinu::Upfile($_FILES['logo']);
        if($logo){
            $data['logo'] = $logo;
        }
        $data['uid']=$uid;
        if(Db::name('link')->insert($data)){
            return 1;
        }else{
            return '添加失败';
        }
    }
}